package Server.Tasks;

import Server.Call;
import Server.PhoneServer;
import Server.PhoneServerThread;

public class HangUpTask extends ServerTask{

	private PhoneServer server;
	
	public HangUpTask (PhoneServerThread p, int priority, String d, PhoneServer server) {
		super(p, priority, d);
		this.server = server;
	}

	public void ProcessTask()
	{
		Call call = phone.getCurrentCall();
		
		if(call != null) {
			if(call.getCaller() == phone) {
				
				if (call.getState() == Call.State.RINGING) {
					call.getCallee().sendMessage(new EndRingingTask(2, null, call.getCallee()));
				} else {
					call.getCallee().sendMessage(new EndCallTask(2, null, call.getCallee()));
				}
				call.endCall();
				
			} else {
				call.endCall();
				call.getCaller().sendMessage(new EndCallTask(2, null, call.getCallee()));
			}
			call.getCaller().setCurrentCall(null);
			call.getCallee().setCurrentCall(null);
			
			server.removeCallFromCallList(call);
			call.CleanUp();
		}				
	}
}
